package com.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import lombok.Builder;
import lombok.Data;

public class TestMylitMap {

	public static void main(String[] args) {
		MyMap a1 = MyMap.builder().code1("c1").code2("d1").code3("e1").values(1).build();
		MyMap a2 = MyMap.builder().code1("c1").code2(null).code3("e1").values(1).build();
		MyMap a3 = MyMap.builder().code1("c2").code2(null).code3("e1").values(1).build();
		MyMap a4 = MyMap.builder().code1("c1").code2("d1").code3("e1").values(1).build();
		MyMap a5 = MyMap.builder().code1("c1").code2("d1").code3("e1").values(1).build();
		List<MyMap> list = new ArrayList<>() {
			{
				add(a1);
				add(a2);
				add(a3);
				add(a4);
				add(a5);
			}
		};
		Function<MyMap, List<Object>> compositeKey = wlb -> Arrays.<Object>asList(wlb.getCode1(), wlb.getCode2()==null?"-":wlb.getCode2(),
				wlb.getCode3());
		Map<Object, List<MyMap>> map = list.stream().collect(Collectors.groupingBy(compositeKey, Collectors.toList()));
		
		List<MyMap> mans = list.stream().filter(j->(j.getCode2()==null?"-":j.getCode2()).equals("-") && j.getCode1().equals("c2")).collect(Collectors.toList());
		System.out.println(mans);
		
		for (Map.Entry<Object, List<MyMap>> entryUser : map.entrySet()) {
			List<Object> key = (List<Object>) entryUser.getKey();
            List<MyMap> entryUserList = entryUser.getValue();
            String code1 = (String) key.get(0);
            String code2 = (String) key.get(1);
            String code3 = (String) key.get(2);
            System.out.println(code1);
            System.out.println(code2);
            System.out.println(code3);
            System.out.println(entryUserList.toString());
            System.out.println("====================");
		}
	}

	@Data
	@Builder
	static class MyMap {
		private String code1;
		private String code2;
		private String code3;
		private Integer values;
	}

}
